package com.ruoyi.produce.process.planoutsource.service;


import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.oa.approve.domain.ApproveDomain;
import com.ruoyi.personnel.oa.approve.mapper.ApproveMapper;
import com.ruoyi.personnel.oa.sequence.service.IApproveSequenceService;
import com.ruoyi.produce.process.planoutsource.domain.ProducePlanOutsource;
import com.ruoyi.produce.process.planoutsource.domain.ProducePlanOutsourceCond;
import com.ruoyi.produce.process.planoutsource.mapper.ProducePlanOutsourceMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 委外生产计划
 * 业务执行层
 * @author Drj 2021/6/28
 */
@Service
public class ProducePlanOutsourceServiceImpl implements  IProducePlanOutsourceService{
    @Autowired
    private ProducePlanOutsourceMapper producePlanOutsourceMapper;
    @Autowired
    private IApproveSequenceService approveSequenceService;
    @Autowired
    private ApproveMapper approveMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 查找委外生产计划
     *
     * @param cond
     * @return
     */
    @Override
    public List<ProducePlanOutsource> selectProducePlanOutsourceList(ProducePlanOutsourceCond cond) {
        return producePlanOutsourceMapper.selectProducePlanOutsourceList(cond);
    }

    /**
     * 根据编号查找指定委外生产计划
     *
     * @param planOutsourceId 委外生产计划编号
     * @return
     */
    @Override
    public ProducePlanOutsource selectProducePlanOutsourceById(String planOutsourceId) {
        return producePlanOutsourceMapper.selectProducePlanOutsourceById(planOutsourceId);
    }

    /**
     * 新增委外生产计划
     *
     * @param producePlanOutsource
     * @return
     */
    @Override
    public int insertProducePlanOutsource(ProducePlanOutsource producePlanOutsource) {
        String planOutsourceId = idUtil.createPlanOutsourceId();
        if (StringUtils.isNotNull(producePlanOutsourceMapper.selectProducePlanOutsourceById(planOutsourceId))){
            //存在
            return 0;
        }
        producePlanOutsource.setPlanOutsourceId(planOutsourceId);
        ApproveDomain approve = new ApproveDomain(planOutsourceId, "委外加工审批", ERPConstant.APPROVAL_TYPE_PRODUCE_OUT_PLAN, producePlanOutsource.getCreateById()); //新增审批
        approve.setCurrentApprover(approveSequenceService.getLowestRoleId(ERPConstant.APPROVAL_TYPE_PRODUCE_OUT_PLAN));
        approveMapper.insertApprove(approve);
        return producePlanOutsourceMapper.insertProducePlanOutsource(producePlanOutsource);
    }

    /**
     * 更新委外生产计划
     *
     * @param producePlanOutsource
     * @return
     */
    @Override
    public int updateProducePlanOutsource(ProducePlanOutsource producePlanOutsource) {
        if (!StringUtils.isNotNull(producePlanOutsourceMapper.selectProducePlanOutsourceById(producePlanOutsource.getPlanOutsourceId()))){
            //不存在
            return 0;
        }
        return producePlanOutsourceMapper.updateProducePlanOutsource(producePlanOutsource);
    }

    /**
     * 删除委外生产计划
     * 仅修改委外生产计划删除标志
     *
     * @param producePlanOutsource
     * @return
     */
    @Override
    public int updateProducePlanOutsourceDeleteFlag(ProducePlanOutsource producePlanOutsource) {
        return producePlanOutsourceMapper.updateProducePlanOutsourceDeleteFlag(producePlanOutsource);
    }
}
